package com.ymatou.iapi.productquery.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productquery.parameter.SecKill.SecKillProductActivityStockBean;
import com.ymatou.iapi.productquery.service.GetSecKillProductActivityStockCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ProductPriceCallService;
import com.ymttest.business.service.ProductQueryCallService;
import com.ymttest.database.mongo.jproduct.ActivityProductsWapper;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;

public class Ts_GetSecKillProductActivityStock {
	private static GetSecKillProductActivityStockCall getSecKillProductActivityStockCall = new GetSecKillProductActivityStockCall();
	private static Integer sellerId = Integer.valueOf(EnvSetup.getData("productpriceSellerId"));
	private static String sellerName = EnvSetup.getData("productpriceSellerName");	
	
	private static YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
	private static ActivityProductsWapper activityProductsWapper = new ActivityProductsWapper();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("取秒杀商品的活动库存量");
	}

	@Before
	public void caseUp() {
		
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getSecKillProductActivityStockCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSecKillProductActivityStock_001() {
		Logger.start(true,"取秒杀商品的活动库存量");
		try{
			//交易不隔离
			String productId = ProductPriceCallService.getProdPriceWithActivity_NoNewBuyerAndNoIsolation(sellerId).getProductId();
			int inaid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("inaid").toString());
			
			
			checkKillProd(productId,inaid);
			
			//交易隔离
			String productId2 = ProductPriceCallService.getProdPriceWithActivity_NoNewBuyerAndIsolation(sellerId).getProductId();
			int inaid2 = Integer.parseInt(activityProductsWapper.getByProdId(productId2).get("inaid").toString());
			
		
			checkKillProd(productId2,inaid2);
			
			
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	public static void checkKillProd(String productId,int prodInActivityId) throws Exception{
		YMTDateUtil.waitTime(3);
		List<SecKillProductActivityStockBean> tgtSecKillProductActivityStockBeanList = ProductQueryCallService.getSecKillProductActivityStockBean(prodInActivityId);
		int activityId = tgtSecKillProductActivityStockBeanList.get(0).getActivityId();
		List<String> catalgoIdList = tgtSecKillProductActivityStockBeanList.stream().map(ele->ele.getCatalogId()).collect(Collectors.toList());
		
		JSONObject ret = getSecKillProductActivityStockCall.search(Arrays.asList(productId,((Integer)activityId).toString()));
		String chkdesc = "GetSecKillProductActivityStock";
		Logger.verifyEquals(200, ret.getInt("Code"), "返回码检查");
		
		//返回值取数据
		List<SecKillProductActivityStockBean> actSecKillProductActivityStockBeanList = new ArrayList<>();
		JSONArray prodListAry = ret.getJSONObject("Data").getJSONArray("StockList");
		for(int i=0;i<prodListAry.length();i++){
			actSecKillProductActivityStockBeanList.add(ProductQueryCallService.getBeanFromJson(prodListAry.getJSONObject(i),SecKillProductActivityStockBean.class));
		}
		
		ProductQueryCallService.compareSecKillProductActivityStockBean(catalgoIdList, tgtSecKillProductActivityStockBeanList, actSecKillProductActivityStockBeanList, chkdesc);
		
	}
	
	
	
}
